//
//  FirstSearchView.m
//  WULIHH
//
//  Created by tang on 16/4/5.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "FirstSearchView.h"
#import "Header.h"


@interface FirstSearchView ()<UITextFieldDelegate>
{
    UITextField *_textField;
}

@end


@implementation FirstSearchView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initUI];
    }
    return self;
}




- (void)initUI
{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WithView, 64.0)];
    [self addSubview:backView];
    [backView setBackgroundColor:uicolor(253, 202,47)];
    
    UIView *lableView = [[UIView alloc] initWithFrame:CGRectMake(20, 27.0, WithView-70.0, 30.0)];
    [lableView setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:lableView];
    [lableView.layer setCornerRadius:15.0];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(6, 6, 17, 17)];
    [imageView setImage:[UIImage imageNamed:@"sousuo.png"]];
    [lableView addSubview:imageView];
    
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(32, 0, lableView.bounds.size.width-17, 30.0)];
    [_textField setPlaceholder:@"搜索您喜爱明星的相关信息"];
    [_textField setFont:[UIFont systemFontOfSize:14.0]];
    [lableView addSubview:_textField];
    [_textField setDelegate:self];
    [_textField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    
    UIButton *cancleButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lableView.frame), 10, WithView-CGRectGetMaxX(lableView.frame), 60.0)];
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [self addSubview:cancleButton];
    [cancleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancleButton.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
    [cancleButton addTarget:self action:@selector(removeSearch) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)removeKeyBoard
{
    [_textField resignFirstResponder];
}


- (void)setFieldText:(NSString *)fieldText
{
    [_textField setText:fieldText];
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_textField resignFirstResponder];
    return YES;
}



-(void)textDidChange:(UITextField *)sender
{
//    NSLog(@"%@", sender.text);
    if (_MFirstSearchViewDelegate && [_MFirstSearchViewDelegate respondsToSelector:@selector(changeText:)]) {
        [_MFirstSearchViewDelegate changeText:sender.text];
    }
}

- (void)removeSearch
{
    if (_MFirstSearchViewDelegate && [_MFirstSearchViewDelegate respondsToSelector:@selector(disMissSearch)])
    {
        [_MFirstSearchViewDelegate disMissSearch];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
